package com.ddx.controller.core;

import com.ddx.common.exception.ResultModel;
import com.ddx.dto.BatchEditProduct;
import com.ddx.dto.ShoppingProductVo;
import com.ddx.service.IShoppingProductService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

/**
 * @author: liyuxiang
 * @create: 2019-01-21
 */
@CrossOrigin(origins = "*", maxAge = 3600)
@RestController
@RequestMapping(value = "supplier/product/")
public class SupplierProductController {

    private Logger logger = LoggerFactory.getLogger(SupplierProductController.class);

    @Autowired
    IShoppingProductService shoppingProductService;

    /**
     * 发布商品
     *
     * @param request
     * @param productVo
     * @return
     */
    @RequestMapping(value = "addProduct")
    public ResultModel addProduct(HttpServletRequest request,@RequestBody ShoppingProductVo productVo) {
        try {
            int userId = Integer.parseInt(request.getHeader("userId"));
            return shoppingProductService.addProduct(userId, productVo);
        } catch (Exception e) {
            logger.error("addProduct error, productVo:{}", e, productVo);
            return ResultModel.getErrorInstance("服务异常，请稍后重试");
        }
    }

    /**
     * 更新商品
     *
     * @param request
     * @param productVo
     * @return
     */
    @RequestMapping(value = "editProduct")
    public ResultModel editProduct(HttpServletRequest request, @RequestBody ShoppingProductVo productVo) {
        try {
            int userId = Integer.parseInt(request.getHeader("userId"));
            return shoppingProductService.editProduct(userId, productVo);
        } catch (Exception e) {
            logger.error("addProduct error, productVo:{}", e, productVo);
            return ResultModel.getErrorInstance("服务异常，请稍后重试");
        }
    }

    /**
     * 商品列表
     *
     * @param request
     * @param map
     * @return
     */
    @RequestMapping(value = "getProductInfo")
    public ResultModel getProductInfo(HttpServletRequest request, @RequestBody Map<String, Object> map) {
        try {
            int userId = Integer.parseInt(request.getHeader("userId"));
            int index = Integer.parseInt(String.valueOf(map.get("index")));
            int size = Integer.parseInt(String.valueOf(map.get("size")));
            int status = Integer.parseInt(String.valueOf(map.get("status")));
            return shoppingProductService.getProductInfo(userId, index, size, status);
        } catch (Exception e) {
            logger.error("addProduct error, map:{}", e, map);
            return ResultModel.getErrorInstance("服务异常，请稍后重试");
        }
    }

    /**
     * 批量上下架
     *
     * @param request
     * @param batchEditProduct
     * @return
     */
    @RequestMapping(value = "batchEdit")
    public ResultModel batchEdit(HttpServletRequest request, @RequestBody BatchEditProduct batchEditProduct) {
        try {
            int userId = Integer.parseInt(request.getHeader("userId"));
            return shoppingProductService.batchEdit(batchEditProduct);
        } catch (Exception e) {
            logger.error("addProduct error, batchEditProduct:{}", e, batchEditProduct);
            return ResultModel.getErrorInstance("服务异常，请稍后重试");
        }
    }

    /**
     * 查询商品详情
     *
     * @param request
     * @param map
     * @return
     */
    @RequestMapping(value = "getProductDetail")
    public ResultModel getProductDetail(HttpServletRequest request, @RequestBody Map<String, Object> map) {
        try {
            int productId = Integer.parseInt(map.get("productId").toString());
            return shoppingProductService.getProductDetail(productId);
        } catch (Exception e) {
            logger.error("getProductDetail error, map:{}", map, e);
            return ResultModel.getErrorInstance("服务异常，请稍后重试");
        }
    }
    /**
     * 查询商品详情
     *
     * @param request
     * @param map
     * @return
     */
    @RequestMapping(value = "getProductDetailH5")
    public ResultModel getProductDetailH5(HttpServletRequest request, @RequestBody Map<String, Object> map) {
        try {
            int productId = Integer.parseInt(map.get("productId").toString());
            return shoppingProductService.getProductDetail(productId);
        } catch (Exception e) {
            logger.error("getProductDetail error, map:{}", map, e);
            return ResultModel.getErrorInstance("服务异常，请稍后重试");
        }
    }
    /**
     * 查询商品详情
     *
     * @param request
     * @param map
     * @return
     */
    @RequestMapping(value = "deleteProduct")
    public ResultModel deleteProduct(HttpServletRequest request, @RequestBody Map<String, Object> map) {
        try {
            int productId = Integer.parseInt(map.get("productId").toString());
            return shoppingProductService.deleteProduct(productId);
        } catch (Exception e) {
            logger.error("getProductDetail error, map:{}", map, e);
            return ResultModel.getErrorInstance("服务异常，请稍后重试");
        }
    }


}
